﻿<?php require_once("includes/session.php"); ?>
<?php confirm_logged_in(); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php access_check(1,$connection); ?>
<?php include("includes/header.php"); ?>

<?php

//variable cleanup
	$vardas = NULL;
	$pavarde = NULL;
	$bylosnr = NULL;
	
if (isset($_POST['Submit'])){
	if (isset($_POST['vardas']) && !empty($_POST['vardas'])){
		//echo "Vardas: " . $_POST['vardas']. "</br>";
		$vardas = $_POST['vardas'];
	};	
	if (isset($_POST['pavarde']) && !empty($_POST['pavarde'])){
		//echo "Pavarde: " . $_POST['pavarde']. "</br>";
		$pavarde = $_POST['pavarde'];
	};
	if (isset($_POST['bylosnr']) && !empty($_POST['bylosnr'])){
		//echo "Bylos Nr: " . $_POST['bylosnr']. "</br>";
		$bylosnr = $_POST['bylosnr'];
	};	
	if (isset($_POST['etape']) && !empty($_POST['etape'])) {
	$etape = "1";
	} else {
	$etape = "0";
	};

	// Normalus query:

	$sql = "SELECT * ";
	$sql .= "FROM `user` ";
	$sql .= "WHERE `user`.etapas = ";
	$sql .= $etape;
	if (isset($_POST['bylosnr']) && !empty($_POST['bylosnr'])){
			//echo "Bylos Nr: " . $_POST['bylosnr']. "</br>";
			$sql .= " AND `user`.bylosnr LIKE '%" . $_POST['bylosnr'] . "%'";
		};	
	//Tikrinam ar yra yrashytas vardas
	if (isset($_POST['vardas']) && ($_POST['vardas']) != null ){
		//echo "</br> YRA VARDAS </br>";
		$sql .= " AND `user`.vardas LIKE ";
		$sql .= "'%".$_POST['vardas']."%'";
	} else { 
		//echo "</br> NERA VARDO </br>";
	};
	//Tikrinam ar yra yrasyta pavarde
	if (isset($_POST['pavarde']) && ($_POST['pavarde']) != null ){
		//echo "</br> YRA pavarde </br>";
		$sql .= " AND `user`.pavarde LIKE ";
		$sql .= "'%".$_POST['pavarde']."%'";
	} else { 
		//echo "</br> NERA Pavardes </br>";
	};
	$sql .= " ORDER BY `user`.`pavarde` ASC";

	
//echo "SQL query: ".$sql . "</br>";
	//Bandom procesinti query
	$result = mysql_query($sql, $connection);
	if ($result) {
		// Success!
		//echo "Successs!! </br>";
	} else {
		// Display error message.
		echo "<p>Subject creation failed.</p>";
		echo "<p>" . mysql_error() . "</p>";
	};
	//xz kas bus
	//$subject = mysql_fetch_array($result);
	//print_r ($subject) ;
$c=1;
				// kazkaip formuotas table'as
			$out = "<table border=\"1\">";
			$out .= "<tr>";
			$out .= "<th>Nr.</th>";
			$out .= "<th>Name</th>";
			$out .= "<th>Surname</th>";
			$out .= "<th>Bylos Nr.</th>";
			$out .= "</tr>";
					  
		 while ($row_result = mysql_fetch_assoc($result)) { 
			$out .= "<tr";
			if (($row_result['etapas']) == 0) {
				$out .=" bgcolor=\"#DDFFCC\"";
			} elseif (($row_result['etapas']) == 1) {
				$out .=" bgcolor=\"#FFFBC1\"";
			};
			$out .= ">";
			$out .= "<td>";
			$out .= $c++;
			$out .= "</td>";
			$out .= "<td>";
			$out .= "<a href=\"summary.php?userid=";
			$out .= $row_result['userid'];
			$out .= "\">";
			$out .= $row_result['vardas'];
			$out .= "</a>";
			$out .= "</td>";
			$out .= "<td>";
			$out .= "<a href=\"summary.php?userid=";
			$out .= $row_result['userid'];
			$out .= "\">";
			$out .= $row_result['pavarde'];
			$out .= "</a>";
			$out .= "</td>";
			$out .= "<td>";
			$out .= $row_result['bylosnr'];
			$out .= "</td>";
			
			$out .= "</tr>";

		 }; 
			$out .= "</table><br>";
				
	
	
};


?>



					<article>
						<h2>Asmenu paieska</h2>
						<p>Pildyk forma</p>
						
					  <form autocomplete="off" action="client.php" method="post" name="search">
					    <p>
					    </p>
					    <table width="640px" border="1">
					      <tr>
					        <td width="70"> Bylos Nr</td>
					        <td width="150"><input name="bylosnr" type="text" autofocus/></td>
					        <td width="60">Vardas</td>
					        <td width="150"><input name="vardas" type="text" /></td>
					        <td width="60">Pavarde</td>
					        <td width="150"><input name="pavarde" type="text" /></td>
				          </tr>
				        </table>
					    Etape
					    <input name="etape" type="checkbox" id="etapas" value="1">
					    <label for="etape"></label>
					    <p>
					      <input name="Submit" type="submit" value="Paieška"/>
					    </p>
					  </form>
    
<?php  
	if (isset($_POST['Submit'])){
	echo $out;
	};
			
?>

					</article>
				
            
<?php require("includes/footer.php"); ?>